<?php
/**
 * @version 1.0
 * @since Aug-2008
 * @package lib
 * @subpackage struct
 * @name CFile
 * @deprecated Lớp thao tác với upload file
*/
class CUpload
{
	/**
	 * Tên của thư mục chứa ảnh
	*/
	public $dir_name;
	/**
	 * Tên thẻ input của file
	*/
	public $input_name;
	/**
	 * Tiền tố cho ảnh tạo ra
	 */
	public $prefix;
	/**
	 * Hàm khởi tạo
	 *
	 * @param string $input_name
	 * @param string $type
	 * @param string $prefix
	 */
	function __construct($dir_name, $input_name, $prefix)
	{
		if(!empty($dir_name) && !empty($input_name) && !empty($prefix)){
			$this->dir_name = $dir_name;
			$this->input_name = $input_name;
			$this->prefix = $prefix;
		}
	}

	/**
	 * Upload File
	 *
	 * @param string $file_input ==>  tên thẻ file (input type='file' name='$requests_file_name')
	 * @param string $prefix  ==> tiếp đầu ngữ cho file tạo ra
	 * @param string$types ==> Những extension của file cho phép upload
	 * @param string $upload_dir ==> Tên thư mục trong thư mục UPLOAD_DIR
	 * @return  mixed
	 */
	function upload($allowed_types = ""){
		static $count;
		if(!is_array($count)) $count = array();
		$upload_dir = $this->dir_name;
		$FILE = $_FILES[$this->input_name];
		$upload_array = true;
		if(!is_array($FILE['name'])){
			$upload_array = false;
			$FILE['name'] = array($FILE['name']);
			$FILE['type'] = array($FILE['type']);
			$FILE['tmp_name'] = array($FILE['tmp_name']);
			$FILE['error'] = array($FILE['error']);
			$FILE['size'] = array($FILE['size']);
		}
		$res = array();
		$errors = array();
		$allowed_types = (!empty($allowed_types)) ? explode(",",$allowed_types) : array();
		foreach($FILE['name'] as $key => $filename){
			if(empty($filename)){
				continue;
			}

			if($FILE['size'][$key]<=0 || $FILE['error'][$key] > 0){
				$errors[] = $key ;
				continue;
			}

			if(strrpos($filename,".") === false){
				$errors[] = $key ;
				continue;
			}
			$filetype = strtolower(substr($filename,strrpos($filename,".")+1));
			if(count($allowed_types) && !in_array($filetype,$allowed_types)){
				$errors[] = $key ;
				continue;
			}
			if($upload_array){
				$uploadfilename = $this->prefix . count($count) . $key . time() . "." . $filetype;
			} else {
				$uploadfilename = $this->prefix . count($count) . time() . "." . $filetype;
			}
			$uploadfile = $upload_dir . $uploadfilename;
			if (move_uploaded_file($FILE['tmp_name'][$key], $uploadfile)) {
				$this->file_treatment($upload_dir, $uploadfilename);
				$res[$uploadfilename] = $uploadfile;
			} else {
				$errors[] = $key ;
			}
		}
		if(!count($errors)){
			$count[count($count)] = count($count);
		} else {
			return false;
		}
		if(!count($res)){
			return NULL;
		}
		return $res;
	}

	function file_treatment($path, $file_name)
	{
	}
	/**
	 * Upload file ảnh
	 *
	 * @param integer $thumb_width
	 * @param integer $thumb_height
	 * @param bool $is_create_thumnail
	 * @param mixed $thumnail_path
	 */
	function upload_image(){
		return $this->upload(IMAGE_EXTENSION);
	}

	/**
	 * Upload media
	 *
	 * @param string $file_input
	 * @param string $prefix
	 * @param string $upload_dir
	 * @return array
	 */
	function upload_media(){
		return $this->upload(MEDIA_EXTENSION);
	}

	/**
	 * Upload flash
	 *
	 * @param string $file_input
	 * @param string $prefix
	 * @param string $upload_dir
	 * @return array
	 */
	function upload_flash(){
		return $this->upload(FLASH_EXTENSION);
	}

	/**
	 * Xóa file
	 *
	 * @param tên file $filename
	 * @return  bool
	 */
	static function delete($filename, $upload_dir=""){
		if(empty($upload_dir)){
			if(strpos($filename, "http://")!==false){
				$filename = strstr($filename, 'upload/');
			}
		}
		if(CFile::exists($upload_dir . $filename)){
			return @unlink($upload_dir . $filename);
		} else {
			return false;
		}
	}

}
/**
 * Hàm tạo đối tượng CFile
 * @return CFile đối tượng CFile mới tạo
 */
function CUpload($dir_name, $input_name, $prefix)
{
	return new CUpload($dir_name,$input_name, $prefix);
}
?>